package com.hq.fxh.No1_Infromation.Infor.Fragment;
/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description : 资讯内容页
 * @Created ${data}
 * @Email xlwenhao1858@sina.com
 */

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.Modle.MyString;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.No1_Infromation.Infor.Activity.NewsDetailsActivity;
import com.hq.fxh.No1_Infromation.Infor.Adapter.Help_Infor_GridView;
import com.hq.fxh.No1_Infromation.Infor.Adapter.Ladapter_infor;
import com.hq.fxh.Modle.SelectByCategoryIdRes;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.Tool;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.CustomerView.PopupWindow_PingBi;
import com.stx.xhb.xbanner.XBanner;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

public class Fragment_infor extends BaseFragment {
    protected View rootView;
    protected PullToRefreshListView listInfor;
    protected XBanner xbanner;
    protected GridView itemGridview;
    View topBannerViewHolder;
    View gridviewViewHolder;
    Ladapter_infor adapter;
    String url2 = "http://img06.tooopen.com/images/20170824/tooopen_sy_222228119642.jpg";
    String url = "http://img06.tooopen.com/images/20170814/tooopen_sy_220490839847.jpg";
    List<SelectByCategoryIdRes.ValueBean> data;
    int pager = 1;


    public void fetchData() {
        LogUtils.e("加载资讯数据");
        UserServiceImpl.selectByCategoryId("s", pager, 20, new Subscriber<SelectByCategoryIdRes>() {
            @Override
            public void onCompleted() {
                listInfor.onRefreshComplete();

            }

            @Override
            public void onError(Throwable e) {
                listInfor.onRefreshComplete();

            }

            @Override
            public void onNext(SelectByCategoryIdRes selectByCategoryIdRes) {
                listInfor.onRefreshComplete();
                if (data == null) {
                    data = new ArrayList<SelectByCategoryIdRes.ValueBean>();
                }

                List<SelectByCategoryIdRes.ValueBean> datalist = selectByCategoryIdRes.getData();
                if (datalist == null) {
                    LogUtils.startToash(getContext(), MyString.nullDataString);
                } else {
                    if (pager == 1) {
                        data.clear();
                    }
                    data.addAll(datalist);
                    adapter.setData(data);
                    adapter.notifyDataSetChanged();

                }

            }
        });

    }

    public static Fragment_infor newInstance() {
        Bundle args = new Bundle();
        Fragment_infor fragment = new Fragment_infor();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_infor_infor;
    }

    @Override
    protected void findView(View rootView) {
        topBannerViewHolder = View.inflate(getContext(), R.layout.xbanner, null);//轮播图
        gridviewViewHolder = View.inflate(getContext(), R.layout.gridview, null);//选项图
        listInfor = (PullToRefreshListView) rootView.findViewById(R.id.list_infor);
        xbanner = (XBanner) topBannerViewHolder.findViewById(R.id.xbanner);
        itemGridview = (GridView) gridviewViewHolder.findViewById(R.id.item_gridview);
        fetchData();
        getData();
    }

    @Override
    protected void initData() {
        listInfor.setMode(PullToRefreshBase.Mode.BOTH);
        listInfor.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pager = 1;
                fetchData();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pager = 1 + pager;
                fetchData();
            }
        });
    }


    public void getData() {
        ArrayList<String> xbannerData = new ArrayList<>();
        xbannerData.add(url);
        xbannerData.add(url2);
        setXbannerData(xbannerData);
        new Help_Infor_GridView(getContext(), itemGridview);
        ListView refreshableView = listInfor.getRefreshableView();
        refreshableView.addHeaderView(topBannerViewHolder);
        refreshableView.addHeaderView(gridviewViewHolder);
        adapter = new Ladapter_infor(new Ladapter_infor.InforCallBack() {

            @Override
            public void onClickPingBi(View view, int position, SelectByCategoryIdRes.ValueBean data) {
                View popview = View.inflate(getContext(), R.layout.popup_information_pingbi, null);
                new PopupWindow_PingBi().showPopupWindow(getActivity(), popview, view);
            }

            @Override
            public void onClickOpenDetail(int position, SelectByCategoryIdRes.ValueBean data) {

                Intent intent = new Intent(getContext(), NewsDetailsActivity.class);
                intent.putExtra("url", data.getSource_url());
                startActivity(intent);
            }
        });
        listInfor.setAdapter(adapter);
    }

    public void setXbannerData(final ArrayList<String> xbannerData) {
        xbanner.setData(xbannerData);
        xbanner.setmAdapter(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, View view, int position2) {
                Tool.setUrlToImage(getContext(), xbannerData.get(position2), (ImageView) view);

            }
        });
        xbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, int position) {
                LogUtils.e("xbanner:" + position);
            }
        });
    }


}
